package com.pudu.pudusclient;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.DataProcess.CanData;
import com.DataProcess.CanDataType;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by fanyp on 2016/9/19.
 */
public class CanbusDataSaveThread extends Thread{

    private static Handler mHandler;
    public static Handler getmHandler(){
        return mHandler;
    }

    @Override
    public void run() {
        Looper.prepare();

        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {

                switch(msg.what){
                    case 0x01:
                        CanData obj = (CanData) msg.obj;
                        SaveCanDataDispatchToFile(obj);
                        break;
                    default:
                        break;
                }
            }
        };
        Looper.loop();
    }

    public static String strCanBusLogPathName = "/sdcard/PuduRobot/CanBus";

    public void SaveCanDataDispatchToFile(CanData data){
        String strValue = "";
        switch(data.type) {
            case CanDataType.SPEED:
                strValue = "\t" + data.speed[0] + "\t" + data.speed[1];
                SaveDataToFile(CanDataType.SPEED, strValue);
                break;
            case CanDataType.UWB:
                strValue = "\t" + data.uwbDistance;
                SaveDataToFile(CanDataType.UWB + data.uwbID, strValue);
                strValue = "\t" + data.uwbDistance2;
                SaveDataToFile(CanDataType.UWB + data.uwbID2, strValue);
                break;
            case CanDataType.IRD:
                strValue = "\t" + Arrays.toString(data.ird);
                SaveDataToFile(CanDataType.IRD, strValue);
                break;
            case CanDataType.PLATE:
                strValue = "\t" + Arrays.toString(data.plate);
                SaveDataToFile(CanDataType.PLATE, strValue);
                break;
            case CanDataType.IMU_YAW:
                strValue = "\t" + data.yaw;
                SaveDataToFile(CanDataType.IMU_YAW, strValue);
                break;
            case CanDataType.FAIL:
                strValue = "\t" + Arrays.toString(data.getData());
                SaveDataToFile("FAIL", strValue);
                break;
            case CanDataType.GYRO:
                strValue = "\t" + Arrays.toString(data.gyroscope);
                SaveDataToFile(CanDataType.GYRO, strValue);
                break;
            default:
                break;
        }
    }

    public void SaveDataToFile(String strCanDataType, String strValue)
    {
        long currentTimeMillis = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentTimeMillis);
        Date date = new Date(currentTimeMillis);
        SimpleDateFormat logfile = new SimpleDateFormat("yyyyMMdd");
        String strLogDatePart = logfile.format(date);

        String strFileNamePath = strCanBusLogPathName + File.separator + strCanDataType + strLogDatePart;
        File file = new File(strFileNamePath);
        boolean bWriteTitle = false;
        try {
            if(file.exists() == false)
            {
                bWriteTitle = true;
                file.createNewFile();
            }

            FileWriter filerWriter = new FileWriter(file, true);
            BufferedWriter bufWriter = new BufferedWriter(filerWriter);

            if(bWriteTitle == true)
            {
                String strTitleValue = strCanDataType;
                bufWriter.write(strTitleValue);
                bufWriter.newLine();
            }

            bufWriter.write(strValue);
            bufWriter.newLine();
            bufWriter.close();
            filerWriter.close();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
